package com.block.channelunion.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.channelunion.model.dto.UnionCountDTO;
import com.block.db.entity.ChannelUnionCountInfo;
import com.block.db.mapper.ChannelUnionCountInfoMapper;
import com.block.channelunion.service.IChannelUnionCountInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 渠道联登统计信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-10-31
 */
@Service
public class ChannelUnionCountInfoServiceImpl extends ServiceImpl<ChannelUnionCountInfoMapper, ChannelUnionCountInfo> implements IChannelUnionCountInfoService {

    @Override
    public void saveUnionCountInfo(UnionCountDTO unionCountDTO){
        ChannelUnionCountInfo unionCountInfo = this.baseMapper.getEntityByCodeAndDateInfo(unionCountDTO.getChannelCode(), unionCountDTO.getDateInfo());
        if (ObjectUtil.isEmpty(unionCountInfo)){
            unionCountInfo = new ChannelUnionCountInfo();
            unionCountInfo.setChannelCode(unionCountDTO.getChannelCode());
            unionCountInfo.setChannelName(unionCountDTO.getChannelName());
            unionCountInfo.setDateInfo(unionCountDTO.getDateInfo());
            boolean save = this.save(unionCountInfo);
            if (!save){
                return;
            }
            unionCountInfo = this.baseMapper.getEntityByCodeAndDateInfo(unionCountDTO.getChannelCode(), unionCountDTO.getDateInfo());
        }

        ChannelUnionCountInfo updateEntity = new ChannelUnionCountInfo();
        updateEntity.setId(unionCountInfo.getId());
        if (StrUtil.equals(unionCountDTO.getStatus(), "CHECK")){
            updateEntity.setCheckNumber(unionCountInfo.getCheckNumber() + 1);
        }else if (StrUtil.equals(unionCountDTO.getStatus(), "REGISTER")){
            updateEntity.setRegisterNumber(unionCountInfo.getRegisterNumber() + 1);
        }else if (StrUtil.equals(unionCountDTO.getStatus(), "SUCCESS")){
            updateEntity.setSuccessNumber(unionCountInfo.getSuccessNumber() + 1);
        }
        boolean b = this.updateById(updateEntity);

    }


}
